package com.springBoot.springBootSysCore.modules.controller.web.system;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.springBoot.springBootSysCore.common.utils.FileUtil;
import com.springBoot.springBootSysCore.modules.entity.system.SysCode;
import com.springBoot.springBootSysCore.modules.entity.system.SysUser;
import com.springBoot.springBootSysCore.modules.entity.utils.UserUtils;
import com.springBoot.springBootSysCore.modules.enums.base.DeleteStateEnum;
import com.springBoot.springBootSysCore.modules.services.system.SysUserService;
import com.springBoot.springBootSysCore.modules.controller.web.base.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("${adminPath}/user")
public class SysUserController extends BaseController<SysUser,String>{

	@Autowired
	private SysUserService sysUserService;

	@ModelAttribute("entity")
	public SysUser get(@RequestParam(required = false) String id) {
		if (StringUtils.isNotBlank(id)) {
			return sysUserService.findById(id);
		} else {
			return new SysUser();
		}
	}

	@RequestMapping("userlis")
	public Object userlis(){
		return "system/user/userList";
	}

    /**
     * 分页查询
     * @param request
     * @param model
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping("queryByPage")
    @ResponseBody
    public Object queryByPage(HttpServletRequest request,
                              Model model) throws JsonProcessingException{
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("EQ_delFlag", DeleteStateEnum.NO_DELETE.getVal());
       return super.queryByPageForLayuiTables(model, request,map, "createDate", "desc");
    }

	@RequestMapping(value="findOne",method=RequestMethod.GET)
	public String findOne(String id,String detail,Model model){
		model.addAttribute("detail",detail);
		return "system/user/userChange";
	}

	@RequestMapping(value="saveEntity",method=RequestMethod.POST)
	@ResponseBody
	public Object saveEntity(SysUser user,HttpServletRequest request) throws Exception {
		Map<String, String> m = FileUtil.upLoadFile("userfiles/user", request);
		if(StringUtils.isNotBlank(m.get("file"))){
			user.setPhoto(m.get("file"));
		}
		return sysUserService.saveSysUser(user,null,null);
	}
	/**
	 * 检查用户名
	 * @return
	 */
	@RequestMapping(value="checkUserName",method=RequestMethod.GET)
	@ResponseBody
	public Object checkUserName(String account,
								@RequestParam(required=false,defaultValue="")String id){
		return sysUserService.checkUserName(account,id);
	}

	/**
	 * 删除--暂未处理级联删除用户信息
	 * @return
	 * @throws JsonProcessingException
	 */
	@RequestMapping(value="deleteEntity",method=RequestMethod.POST)
	@ResponseBody
	public Object deleteEntity(SysUser user) throws JsonProcessingException{
		return sysUserService.delete(user);
	}

	/**
	 * 修改锁定状态
	 * @return
	 */
	@RequestMapping(value="updateAvailableById",method=RequestMethod.POST)
	@ResponseBody
	public Object updateStateById(String id,String available){
		return sysUserService.updateStateById(id,available);
	}

	/**
	 * 重置密码
	 * @param id
	 * @param password
	 * @return
	 */
	@RequestMapping(value="resetLoginPwd",method=RequestMethod.POST)
	@ResponseBody
	public Object resetLoginPwd(String id,@RequestParam(required = false,defaultValue = "111111") String password){
		return sysUserService.resetLoginPwd(id,password);
	}


}
